import CTMapOl from "@ct/ct_map_ol";
const mapUrl = {
  // 标准图层
  basic:'http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403',
  // 卫星图层
  satellite:'http://t0.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403',
  // 标注图层
  marker:'http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403'
}
export default mapUrl;

export function transCoordinate(coordinate, fromType = 'EPSG:4326', toType = 'EPSG:3857') {
  return CTMapOl.extend.formatLayer.transCoordinate(coordinate, fromType, toType)
 }
/**
 * 地图弹窗
 */
export function eventOpenInfoWindow(map, lng, lat, content, popClass, offsetX, offsetY, infoWindowId= 'infoWindowId',positioning = 'bottom-center' ){
  let infoWindow;
  let $info;

  $info = document.createElement('div');
  $info.id = infoWindowId;
  $info.className = popClass;
  $info.innerHTML = content;
  $info.onmousewheel = function(e) {
    e.stopPropagation();
  };
  document.body.appendChild($info);

  // 单坐标转换指定投影系
  let coordinate = transCoordinate([lng, lat]);
  /**
   * Create an overlay to anchor the popup to the map.
   */
  infoWindow = new CTMapOl.Overlay({
    id: infoWindowId,
    element: $info,
    position: coordinate,
    positioning: positioning,      //相对于其位置属性的实际位置
    stopEvent: true,                   //事件冒泡
    offset: [offsetX, offsetY],
    // autoClose: false, // 设置自动关闭为false
    // pointerEvents: 'auto', // 设置pointer-events为auto
    // autoPan: {
    //   animation: {
    //     duration: 0,
    //   },
    // },
    zIndex: 100
  });
  map.addOverlay(infoWindow);
  return infoWindow;
}

/**
 * 关闭地图弹窗
 */
export function eventCloseInfoWindow(map, infoWindowIns){
  if(map.removeOverlay) {
    map.removeOverlay(infoWindowIns);
  }
}
